/*
 * This file is part of aion-unique <aion-unique.com>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionemu.gameserver.network.aion.serverpackets;

import javolution.util.FastMap;
import javolution.util.FastMap.Entry;

import com.aionemu.gameserver.model.drop.DropItem;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.network.aion.AbstractAionServerPacket;
import com.aionemu.gameserver.network.aion.GameClient;

/**
 * 
 * @author Lyahim, alexa026, Avol, Corrected by Metos
 * 
 * modified by ATracer
 * 
 */
public class SM_LOOT_ITEMLIST extends AbstractAionServerPacket<GameClient>
{
	private int			targetObjectId;
	private FastMap<Integer, DropItem>		dropItems;

	public SM_LOOT_ITEMLIST(int targetObjectId, FastMap<Integer,DropItem> dropItems, Player player)
	{
		this.targetObjectId = targetObjectId;
		this.dropItems = FastMap.newInstance();
		if (dropItems == null)
			return;
		for (Entry<Integer, DropItem> e = dropItems.head(), end = dropItems.tail(); (e = e.getNext()) != end;)
		{
			if (e.getValue().getPlayerObjId() == 0 || player.getObjectId() == e.getValue().getPlayerObjId())
				this.dropItems.put(e.getKey(), e.getValue());
		}
	}

	/**
	 * {@inheritDoc} dc
	 */
	@Override
	protected void writeImpl(GameClient client)
	{
		writeD(targetObjectId);
		writeC(dropItems.size());

		for (Entry<Integer, DropItem> e = dropItems.head(), end = dropItems.tail(); (e = e.getNext()) != end;)
		{
			writeC(e.getKey()); // index in droplist
			writeD(e.getValue().getDropTemplate().getItemId());
			writeH((int) e.getValue().getCount());
			writeD(0);
		}
		FastMap.recycle(dropItems);
		dropItems = null;
	}
}
